package com.qzy.demo.controller;

import com.alibaba.fastjson.JSON;
import com.qzy.demo.pojo.StudentPojo;
import com.qzy.demo.service.SolrService;
import com.qzy.demo.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.MapSolrParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@Slf4j
@Controller
@RequestMapping("/solr")
public class SolrController {

    @Autowired
    private SolrClient solrClient;
    @Autowired
    private StudentService studentService;
    @Autowired
    private SolrService solrService;

    /**
     * solr 查询
     * @param stuname
     * @return
     * @throws IOException
     * @throws SolrServerException
     */
    @RequestMapping("/quryBystuname")
    @ResponseBody
    public String quryBystuname(String stuname) throws IOException, SolrServerException {
        // 创建搜索对象
        SolrQuery query = new SolrQuery();
        //2 封装查询参数
        Map<String, String> queryParamMap = new HashMap<String, String>();
        //queryParamMap.put("q", "id:"+stuname);
        queryParamMap.put("q", "id:"+stuname+" and gid:"+stuname);
        //设置每页显示多少条
        queryParamMap.put("rows","3");
        //3 添加到SolrParams对象,SolrParams 有一个 SolrQuery 子类，它提供了一些方法极大地简化了查询操作
        MapSolrParams queryParams = new MapSolrParams(queryParamMap);
        //发起搜索请求
        QueryResponse response = solrClient.query(queryParams);
        // 查询结果
        SolrDocumentList docs = response.getResults();
        // 查询结果总数
        long cnt = docs.getNumFound();
        System.out.println("总条数为"+cnt+"条");
      //  solrClient.close();
        return JSON.toJSONString(docs);
    }

    /**
     * 创建solr索引 非数据库导入
     * @return
     */
    @RequestMapping("/createIndex")
    @ResponseBody
    public String createIndex(){
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("sid",String.valueOf(16));
        doc.addField("stuname","陈乐");
        doc.addField("sex","男");
        doc.addField("address","江苏盐城");
        doc.addField("birthday","19931210");
        try {
             solrClient.add(doc);
             solrClient.commit();
        }catch(Exception e){
            e.printStackTrace();
            return "error";
        }
        return "ok";
    }
    /**
     * 创建solr索引 数据库导入
     * @return
     */
    @RequestMapping("/createIndexFromTable")
    @ResponseBody
    public String createIndexFromTable(){
       try{
           solrService.importAllSolrIndex();
       }catch(Exception e){
           e.printStackTrace();
           return "error";
       }
        return "ok";
    }
}
